package service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Service;

import dao.BaseDao;
import dao.BaseDaoImpl;
import pojo.Tb_subject;
import pojo.Tb_user;
@Service("SubjectService")
public class SubjectServiceImpl implements SubjectService{
	BaseDao<Tb_subject>bd=new BaseDaoImpl<Tb_subject>();
	public List<Tb_subject> findpage(int first) {
		Query q=bd.findPage("from Tb_subject", null);
		q.setFirstResult(first*10);
		q.setMaxResults(first*10+10);
		List<Tb_subject> list=q.list();
		return list;
	}

	public void insert(Tb_subject sub) {
		bd.insert(sub);
	}

	public void delete(int id) {
		// TODO Auto-generated method stub
		
	}

	public void update(Tb_subject sub) {
		StringBuffer sb=new StringBuffer("update Tb_subject set");
		List<Object>param=new ArrayList<Object>();
		
		if(sub.getName()!=null&&!sub.getName().equals("")){
			param.add(sub.getName());
			sb.append(" name=?");
		}
		if(sub.getDescription()!=null&&!sub.getDescription().equals("")){
			param.add(sub.getDescription());
			sb.append(",description=?");
		}
		
		String sql=sb.toString();
		bd.update(sql+" where id="+sub.getId(), param);
		
	}

	public List<Tb_subject> findall() {
		return bd.find("from Tb_subject", null);
	}

	public Tb_subject findById(int id) {
		Tb_subject u=bd.findById(Tb_subject.class, id);
		return u;
		
	}

}
